<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get Rules : DataMapper ORM - User Guide</title>

<link rel="shortcut icon" type="image/png" href="../images/favicon.png" />
<link rel="stylesheet" type="text/css" media="all" href="../css/userguide.css" />
<link rel="alternate" type="application/rss+xml" title="Datamapper ORM Updates Feed" href="/rss.xml" />

<meta http-equiv="expires" content="-1" />
<meta http-equiv= 'pragma' content="no-cache" />
<meta name="robots" content="all" />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper ORM</h1></td>
<td id="breadcrumb_right"><a href="toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="/">Datamapper ORM Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Get Rules
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Get Rules</h1>

<p>Just like <a href="validation.html">Validation Pre-Processing</a> rules, DataMapper supports the ability to have rules that are run immediately after <var><u>get</u></var>ting an object.</p>
<p>The purpose of these rules is to help with the transition between database value formats and object value formats.
	Some examples of where you might want a get rule are:</p>
<ul>
	<li>Automatically converting a <strong>date</strong> field into a Unix timestamp.</li>
	<li>Converting an object that has been stored in the database as a <strong>JSON</strong> or <strong>serialized</strong> string back into an object.</li>
	<li>Fixing Postgres <strong>boolean</strong> columns, which are returned as <dfn>'t'</dfn> or <dfn>'f'</dfn>, instead of <dfn>1</dfn> or <dfn>0</dfn>, or <dfn>TRUE</dfn> or <dfn>FALSE</dfn>.</li>
</ul>

<p>
	Get rules are stored in the validation array.
	They look and work exactly like <a href="validation.html#Rules">validation rules</a>, except you store them under the <dfn>'get_rules'</dfn> key, instead of the <dfn>'rules'</dfn> keys.
	Also, unlike validation rules, they are <strong>never skipped</strong>.
</p>

<h2>Setting Get Rules</h2>
<p>Simply include the rules you want to run in the <dfn>'get_rules'</dfn> key on the <var><i>$validation</i></var> field.</p>
<pre>
<kbd>var </kbd><var><i>$validation</i> </var><kbd>= array(
    </kbd><dfn>'startdate' </dfn><kbd>=&gt; array(
        </kbd><dfn>'label' </dfn><kbd>=&gt; </kbd><dfn>'Start Date'</dfn><kbd>,
        </kbd><dfn>'rules' </dfn><kbd>=&gt; array(</kbd><dfn>'required'</dfn><kbd>, </kbd><dfn>'valid_date'</dfn><kbd>),
        </kbd><dfn>'get_rules' </dfn><kbd>=&gt; array(</kbd><dfn>'strtotime'</dfn><kbd>)
    )
);</kbd>
</pre>

<h2>Automatic Get Rules</h2>
<p>
	Datamapper ORM automatically runs <var><b>intval</b></var> on the <var><i>$id</i></var> field, as well as any in-table foreign keys.
 	This was done to prevent errors when saving back a modified value to the database, since PHP sometimes receives a string for integer values.
</p>

<h2>Included Get Rules</h2>
<p>
	Datamapper ORM does not include any special, built-in rules.  However, you can use any non-related rule that is supported by Validation.
	These methods are probably the most useful:
</p>
<ul>
	<li><strong>intval</strong>: Forces a string value to be an integer.</li>
	<li><strong>floatval</strong>: Forces a string value to be a floating number.</li>
	<li><strong>strtotime</strong>: Forces conversion of a string into a Unix timestamp.</li>
</ul>

<h2>Custom Get Rules</h2>
<p>
	Please see <a href="validation.html#Custom.Rules">Custom Validation</a> for how to write your own methods.
	Get rules follow the exact same standards, except for two differences:
</p>
<ul>
	<li>Return values are ignored.</li>
	<li>Get Rules should not set an error.</li>
</ul>

</div>
<!-- END CONTENT -->


<div id="footer">
<p>
<span id="footer_previous">Previous Topic:&nbsp;&nbsp;<a href=""></a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;</span>
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>
<span id="footer_next">&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href=""></a></span>
</p>
<div id="copyrights">
<p><a href="/">Datamapper ORM</a> &nbsp;&middot;&nbsp; Copyright &copy; 2010-2011 &nbsp;&middot;&nbsp; Harro "WanWizard" Verton</p>
<p><a href="license.html">Other License Information</a></p>
</div>
</div>

<script type="text/javascript" src="../js/mootools.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../',
			pagespath: ''
		});

	});
//-->
</script>
</body>
</html>
